<!--
  功能：预约列表
  作者：zzb
  邮箱：2283325110@qq.com
  时间：2021年12月06日 17:05:38
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class="">
    <vab-query-form-left-panel>
      <el-form
        ref="form"
        :model="queryForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-date-picker
            v-model="appointDate"
            type="date"
            placeholder="选择日期"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form-left-panel>
    <el-table
      ref="tableSort"
      :data="tableData"
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
      style="width: 100%"
      :border="true"
      :row-style="{
        fontSize: '12px',
        color: '#666',
        fontFamily: 'MicrosoftYaHeiUI',
      }"
    >
      <el-table-column
        prop="appointDate"
        label="日期"
        width="180"
      ></el-table-column>
      <el-table-column prop="time" label="时间段" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column
        prop="whichPin"
        label="针剂"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="vaccine"
        label="疫苗"
        width="180"
      ></el-table-column>
      <el-table-column prop="appointSite" label="接种点"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="240px">
        <template #default="{ row }">
          <el-button type="warning" @click="handleEdit(row)">修改</el-button>
          <el-button type="danger" @click="handleDelete(row)">取消</el-button>
          <el-button type="primary" @click="getInoculation(row)">
            确认接种
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-show="tableData == null" description="暂无预约"></el-empty>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="5"
      :page-sizes="[3, 5, 7, 9]"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <form-edit ref="edit" @save="fetchData"></form-edit>
  </div>
</template>

<script>
  import {
    findAllAppointment,
    cancelAppointment,
    queryPageAppointment,
  } from '@/api/appoint'
  import { addOneHistory, updateHistory } from '@/api/history'
  import { findUserByName } from '@/api/user'
  import FormEdit from './components/FormEdit.vue'
  export default {
    // 组件名称
    name: 'MyAppoint',
    components: {
      FormEdit,
    },
    // 组件参数 接收来自父组件的数据
    props: {},
    // 组件状态值
    data() {
      return {
        appointDate: '',
        tableData: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        queryForm: {
          pageNum: 1,
          pageSize: 5,
        },
      }
    },
    // 侦听器
    watch: {},
    // 以下是生命周期钩子
    /**
     * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
     */
    created() {
      this.fetchData()
    },
    /**
     * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
     * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
     */
    mounted() {},
    // 组件方法
    methods: {
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async handleQuery() {
        let d = new Date(this.appointDate),
          month = '' + (d.getMonth() + 1),
          day = '' + d.getDate(),
          year = d.getFullYear()
        if (month.length < 2) month = '0' + month
        if (day.length < 2) day = '0' + day
        this.appointDate = [year, month, day].join('-')
        const { data } = await findAllAppointment()
        if (this.appointDate == '1970-01-01') {
          this.fetchData(this.queryForm)
        } else
          this.tableData = data.filter((item) => {
            return item.appointDate == this.appointDate
          })
        // console.log(this.appointDate)
      },
      cancel(userName) {
        cancelAppointment(userName).then((res) => {
          this.$baseMessage(res.msg, 'success')
          this.fetchData()
        })
      },
      async fetchData() {
        this.tableData = []
        const { data } = await queryPageAppointment(this.queryForm)
        if (data.list == null) return
        this.tableData = data.list
        this.total = data.total
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        this.$confirm('此操作将删除该预约, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.cancel(row.userName)
            this.fetchData()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '操作取消',
            })
          })
      },
      getInoculation(row) {
        cancelAppointment(row.userName).then(() => {
          findUserByName(row.userName).then((res) => {
            if (row.whichPin == '第一针') {
              let firstPin = 1
              let firstDate = new Date()
              let month = '' + (firstDate.getMonth() + 1)
              let day = '' + firstDate.getDate()
              let year = firstDate.getFullYear()
              if (month.length < 2) month = '0' + month
              if (day.length < 2) day = '0' + day
              let firstTime = [year, month, day].join('-')
              addOneHistory({
                age: res.data.age,
                firstDate: firstTime,
                firstPin: firstPin,
                gender: row.gender,
                name: row.name,
                phone: res.data.phone,
                secondDate: null,
                secondPin: 1,
                userName: row.userName,
              })
            } else {
              let secondDate = new Date()
              let month = '' + (secondDate.getMonth() + 1)
              let day = '' + secondDate.getDate()
              let year = secondDate.getFullYear()
              if (month.length < 2) month = '0' + month
              if (day.length < 2) day = '0' + day
              let secondTime = [year, month, day].join('-')
              updateHistory({
                secondDate: secondTime,
                secondPin: 1,
                firstPin: 1,
                userName: row.userName,
              })
            }
          })
          this.fetchData()
        })
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped></style>
